gl: Use older GLSL shaders with legacy contexts
authorEmmanuele Bassi <ebassi@gnome.org>
Tue, 6 Oct 2015 18:55:43 +0000 (19:55 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Wed, 7 Oct 2015 15:21:57 +0000 (16:21 +0100)
If we're using modern GLSL, then we should stop using deprecated
modifiers, like 'varying' and 'attribute', as well as deprecated global
variables, like 'gl_FragColor'.

On the other hand, with legacy contexts we should be using older GLSL
shaders, to maximize compatibility.

https://bugzilla.gnome.org/show_bug.cgi?id=756142

gdk/gdkgl.c

index 85ce63da559c8ff0a6cfd1f6db7d3980fe18624d..81eb40dc66008da258fbea867e7b43e923849433 100644 (file)
@@ -147,9 +147,27 @@ bind_vao (GdkGLContextPaintData *paint_data)
 static void
 use_texture_2d_program (GdkGLContextPaintData *paint_data)
 {
-  static const char *vertex_shader_code =
+  static const char *vertex_shader_code_150 =
     "#version 150\n"
     "uniform sampler2D map;"
+    "in vec2 position;\n"
+    "in vec2 uv;\n"
+    "out vec2 vUv;\n"
+    "void main() {\n"
+    "  gl_Position = vec4(position, 0, 1);\n"
+    "  vUv = uv;\n"
+    "}\n";
+  static const char *fragment_shader_code_150 =
+    "#version 150\n"
+    "in vec2 vUv;\n"
+    "out vec4 vertexColor;\n"
+    "uniform sampler2D map;\n"
+    "void main() {\n"
+    "  vertexColor = texture2D (map, vUv);\n"
+    "}\n";
+  static const char *vertex_shader_code_130 =
+    "#version 130\n"
+    "uniform sampler2D map;"
     "attribute vec2 position;\n"
     "attribute vec2 uv;\n"
     "varying vec2 vUv;\n"
@@ -157,14 +175,21 @@ use_texture_2d_program (GdkGLContextPaintData *paint_data)
     "  gl_Position = vec4(position, 0, 1);\n"
     "  vUv = uv;\n"
     "}\n";
-  static const char *fragment_shader_code =
-    "#version 150\n"
+  static const char *fragment_shader_code_130 =
+    "#version 130\n"
     "varying vec2 vUv;\n"
     "uniform sampler2D map;\n"
     "void main() {\n"
     "  gl_FragColor = texture2D (map, vUv);\n"
     "}\n";
 
+  const char *vertex_shader_code = paint_data->is_legacy
+                                 ? vertex_shader_code_130
+                                 : vertex_shader_code_150;
+  const char *fragment_shader_code = paint_data->is_legacy
+                                   ? fragment_shader_code_130
+                                   : fragment_shader_code_150;
+
   if (paint_data->texture_2d_quad_program.program == 0)
     make_program (&paint_data->texture_2d_quad_program, vertex_shader_code, fragment_shader_code);
 
@@ -178,9 +203,9 @@ use_texture_2d_program (GdkGLContextPaintData *paint_data)
 static void
 use_texture_rect_program (GdkGLContextPaintData *paint_data)
 {
-  static const char *vertex_shader_code =
+  static const char *vertex_shader_code_150 =
     "#version 150\n"
-    "uniform sampler2DRect map;"
+    "uniform sampler2DRect map;\n"
     "attribute vec2 position;\n"
     "attribute vec2 uv;\n"
     "varying vec2 vUv;\n"
@@ -188,13 +213,37 @@ use_texture_rect_program (GdkGLContextPaintData *paint_data)
     "  gl_Position = vec4(position, 0, 1);\n"
     "  vUv = uv;\n"
     "}\n";
-  static const char *fragment_shader_code =
+  static const char *fragment_shader_code_150 =
     "#version 150\n"
     "varying vec2 vUv;\n"
     "uniform sampler2DRect map;\n"
     "void main() {\n"
     "  gl_FragColor = texture2DRect (map, vUv);\n"
     "}\n";
+  static const char *vertex_shader_code_130 =
+    "#version 130\n"
+    "uniform sampler2DRect map;\n"
+    "attribute vec2 position;\n"
+    "attribute vec2 uv;\n"
+    "varying vec2 vUv;\n"
+    "void main() {\n"
+    "  gl_Position = vec4(position, 0, 1);\n"
+    "  vUv = uv;\n"
+    "}\n";
+  static const char *fragment_shader_code_130 =
+    "#version 130\n"
+    "varying vec2 vUv;\n"
+    "uniform sampler2DRect map;\n"
+    "void main() {\n"
+    "  gl_FragColor = texture2DRect (map, vUv);\n"
+    "}\n";
+
+  const char *vertex_shader_code = paint_data->is_legacy
+                                 ? vertex_shader_code_130
+                                 : vertex_shader_code_150;
+  const char *fragment_shader_code = paint_data->is_legacy
+                                   ? fragment_shader_code_130
+                                   : fragment_shader_code_150;
 
   if (paint_data->texture_rect_quad_program.program == 0)
     make_program (&paint_data->texture_rect_quad_program, vertex_shader_code, fragment_shader_code);